let os = uni.getStorageSync('osName')

let msg = function(message) {
	uni.showToast({
		title: message,
		duration: 2000,
		icon: "none"
	});
}
let phoneVerify = function(phoneNumber) {
	if (phoneNumber.length !== 11) { // 判断是否为11位数字
		msg("手机号格式不正确")
		return false;
	}
	for (var i = 0; i < phoneNumber.length; i++) {
		if (phoneNumber.charAt(i) < "0" || phoneNumber.charAt(i) > "9") { // 判断是否为数字
			msg("手机号格式不正确")
			return false;
		}
	}
	if (phoneNumber.charAt(0) !== "1") { // 判断首位是否为1
		msg("手机号格式不正确")
		return false;
	}
	return true; // 满足以上条件则为合法手机号码
}

let toUrl = function(url, type = 'navigateTo') {
	if (type == 'navigateTo') {
		uni.navigateTo({
			url: url
		})
	}
	if (type == 'redirectTo') {
		uni.redirectTo({
			url: url
		})
	}
	if (type == 'reLaunch') {
		uni.reLaunch({
			url: url
		})
	}
}



let idcardVerify = function(idcard) {
	var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
	return p.test(idcard)
}
let phoneType = function() {
	var type = ""
	uni.getSystemInfo({
		success: res => {
			type = res.osName
		}
	})
	return type
}

let back = function(delta = 1) {
	uni.navigateBack({
		delta: delta
	})
}

let upload = function(type, file, callback, add_water = 0) {
	uni.showLoading({
		title: '上传中',
	})
	const token = uni.getStorageSync('token')
	const cid = uni.getStorageSync('cid') || '';
	const terminal = uni.getStorageSync('terminal') || '';
	uni.uploadFile({
		url: http.baseUrl + 'upload/permanent', //仅为示例，非真实的接口地址
		filePath: file,
		header: {
			'token': token,
			'cid': cid,
			'terminal': terminal,
		},
		name: 'file',
		formData: {
			'type': type,
			add_water: add_water,
		},
		success: (uploadFileRes) => {
			uni.hideLoading()
			let res = JSON.parse(uploadFileRes.data)
			if (res.code == 0) {
				if (res.data.status == 2 || res.data.status == 1) {
					if (res.data.status == 1) {
						msg(res.data.msg);
					}
					callback({
						code: 0,
						data: {
							...res.data.result
						}
					})
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			} else {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
			}
		},
		fail(err) {
			uni.showToast({
				title: `${err}`,
				icon: 'none'
			})
			uni.hideLoading();
		},
		complete() {
			uni.hideLoading()
		}
	});
}


let previewImage = function(urls, current = 0) {
	uni.previewImage({
		current: current,
		urls: urls,
	})
}

let backAction = function() {
	if (uni.getSystemInfoSync().platform == 'android') {

	} else {
		if (window.webkit) {
			// 调原生回退
			window.webkit.messageHandlers.backAction.postMessage('');
		} else {
			msg('未找到 window.webkit')
		}
	}

}

let getAvatar = function(imgs) {
	if (Object.prototype.toString.call(imgs) === '[object Array]') {
		return imgs[0];
	} else {
		if (imgs.indexOf(',') > -1) {
			return imgs.split(',')[0];
		} else {
			return imgs;
		}
	}

}

let prefix = function(imgs) {
	return `https://qiniu-cdn.maeiyun.com//h5static/${imgs}`;
}

let isIos = function() {
	if (uni.getSystemInfoSync().platform == 'android') {
		return false;
	} else {
		return true;
	}
}

let getUrl = function(url) {
	
	// 测试域名
	// return `https://dev2.lefujin.cn/home/#${url}`;
	
	// 正式环境的测试域名
	// return `https://www.tjywxk.top/home/#${url}`;
	
	// 废弃域名
	// return `https://pay.lefujin.cn${url}`;
	
	// 正式域名
	return `https://pay-v2.lefujin.cn/v717${url}`;
}

let onCopy = function(str) {
	uni.setClipboardData({
		data: `${str}`,
		showToast: false,
		success: () => {
			msg('已复制到剪贴板');
		}
	});
}




export default {
	msg,
	phoneVerify,
	idcardVerify, // 身份证校验
	toUrl, // 跳转
	back,
	upload,
	previewImage, // 图片预览
	backAction, // 原生返回
	getAvatar,
	prefix, // 七牛云前缀
	getUrl, // 域名前缀
	onCopy, // 复制
}